package com.zf.shuashua.ui.popup;

import android.animation.ValueAnimator;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.zf.shuashua.R;

/**
 * 好友助力
 */
public class FriendInvitationPopup extends Dialog {

    private ImageView imgReceive, imgAvatar;
    private ImageView imgClose;
    private TextView tvName;
    private String avatar;
    private String name;
    private OnPopupListener mListener;

    private ValueAnimator receiveAnim;

    public FriendInvitationPopup(@NonNull Context context, String avatar, String name) {
        super(context);
        this.avatar = avatar;
        this.name = name;
    }

    public void close() {
        dismiss();
    }

    public void setPopupListener(OnPopupListener listener) {
        this.mListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_friend);
        try {
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        } catch (Exception ignored) {

        }
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setupView();
        setupData();
    }

    private void setupView() {
        imgClose = findViewById(R.id.img_close);
        imgClose.setOnClickListener(v -> {
            dismiss();
            if (mListener != null) {
                mListener.onClose();
            }
        });

        imgReceive = findViewById(R.id.iv_receive);

        imgReceive.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onNext();
            }
            dismiss();
        });

        imgAvatar = findViewById(R.id.imgAvatar);
        tvName = findViewById(R.id.tv_name);
    }

    private void setupData(){
        tvName.setText(name);
        Glide.with(getContext()).load(avatar)
                .error(R.mipmap.ic_avatar_default)
                .placeholder(R.mipmap.ic_avatar_default)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))//圆形
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imgAvatar);
    }

    @Override
    public void show() {
        super.show();
        if (receiveAnim != null && receiveAnim.isRunning()) {
            receiveAnim.end();
        }
        receiveAnim = ValueAnimator.ofFloat(0.9F, 1F);
        receiveAnim.setDuration(300);
        receiveAnim.setRepeatCount(-1);
        receiveAnim.setRepeatMode(ValueAnimator.REVERSE);
        receiveAnim.setInterpolator(new AccelerateDecelerateInterpolator());
        receiveAnim.addUpdateListener(animation -> {
            float value = (float) animation.getAnimatedValue();
            imgReceive.setScaleX(value);
            imgReceive.setScaleY(value);
        });
        receiveAnim.start();
    }

    @Override
    public void dismiss() {
        super.dismiss();
        if (receiveAnim != null && receiveAnim.isRunning()) {
            receiveAnim.end();
        }
    }
}

